﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XEvents.Library
{
    public class EventSessionCollection : List<EventSession>
    {
        public EventSessionCollection(IEnumerable<EventSession> ses)
            : base(ses)
        { }

        public EventSession this[string name]
        {
            get { return this.ToDictionary(n => n.Name, n => n)[name]; }
        }


        public System.Xml.XmlDocument Refresh(string name)
        {
            EventSession es = this[name];

            Internal.EventSessionData esd = new Internal.EventSessionData(es.Server);

            es.Server.sessiondata.server_event_sessions.RemoveAll(s => s.name == name);
            es.Server.sessiondata.server_event_session_targets.RemoveAll(s => s.event_session_id == es.EventSessionId);
            es.Server.sessiondata.server_event_session_fields.RemoveAll(s => s.event_session_id == es.EventSessionId);
            es.Server.sessiondata.server_event_session_events.RemoveAll(s => s.event_session_id == es.EventSessionId);
            es.Server.sessiondata.server_event_session_actions.RemoveAll(s => s.event_session_id == es.EventSessionId);
            es.Server.sessiondata.sessions.RemoveAll(s => s.name == name);

            es.Server.sessiondata.server_event_sessions.AddRange(from st in esd.server_event_sessions
                                                                 where st.name == name
                                                                 select st);
            es.Server.sessiondata.server_event_session_actions.AddRange(from st in esd.server_event_session_actions
                                                                        where st.event_session_id == es.EventSessionId
                                                                        select st);
            es.Server.sessiondata.server_event_session_targets.AddRange(from st in esd.server_event_session_targets
                                                                        where st.event_session_id == es.EventSessionId
                                                                        select st);
            es.Server.sessiondata.server_event_session_fields.AddRange(from st in esd.server_event_session_fields
                                                                        where st.event_session_id == es.EventSessionId
                                                                       select st);
            es.Server.sessiondata.server_event_session_events.AddRange(from st in esd.server_event_session_events
                                                                        where st.event_session_id == es.EventSessionId
                                                                       select st);
            es.Server.sessiondata.sessions.AddRange(from st in esd.sessions
                                                                       where st.name == es.name
                                                    select st);

            IEnumerable<EventSession> rets = (from ses in esd.server_event_sessions
                                 where ses.name == name
                                 select new EventSession(ses, es.Server));

            if (rets.Count() == 0)
                this.Remove(es);
            else
                es = rets.ElementAt(0);

            return Utilities.GetSerializedXml(typeof(EventSession), es);
        }
    }
}
